from GetStations import *
from fake_useragent import UserAgent

data = []
type_data = []
headers = {"User-Agent": UserAgent().random, "Cookie":
    "_uab_collina=171988250190771557838894; "
    "JSESSIONID=165D83EDA183611FF1C2599ECE9C1636; "
    "guidesStatus=off; highContrastMode=defaltMode; "
    "cursorStatus=off; _jc_save_fromStation=%u91CD%u5E86%2CCQW; "
    "_jc_save_toStation=%u6210%u90FD%2CCDW; "
    "_jc_save_fromDate=2024-07-02; _jc_save_toDate=2024-07-02; "
    "_jc_save_wfdc_flag=dc; "
    "route=c5c62a339e7744272a54643b3be5bf64; "
    "BIGipServerotn=2715222282.64545.0000"}


def Query(date, from_station, to_station):
    url = ("https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={}&leftTicketDTO.from_station={"
           "}&leftTicketDTO.to_station={}&purpose_codes=ADULT").format(date, from_station, to_station)
    response = requests.get(url, headers=headers)
    result = response.json()
    result = result['data']['result']

    if IsStations():
        stations = eval(Read())
        if len(result) != 0:
            for i in result:
                tmp_list = i.split('|')
                from_station = list(stations.keys())[list(stations.values()).index(tmp_list[6])]
                to_station = list(stations.keys())[list(stations.values()).index(tmp_list[7])]

                seat = [tmp_list[3], from_station, to_station, tmp_list[8], tmp_list[9],
                        tmp_list[10], tmp_list[32], tmp_list[31],
                        tmp_list[30], tmp_list[21], tmp_list[23],
                        tmp_list[33], tmp_list[28],
                        tmp_list[24], tmp_list[29], tmp_list[26]]
                new_seat = []
                for s in seat:
                    if s == "":
                        s = "--"
                    else:
                        s = s
                    new_seat.append(s)
                data.append(new_seat)

        return data

def GetGVehicle():
    if len(data) != 0:
        for g in data:
            i = g[0].startswith('G')
            if i:
                type_data.append(g)


def RemoveGVehicle():
    if len(data) != 0 and len(type_data) != 0:
        for g in data:
            i = g[0].startswith('G')
            if i:
                type_data.remove(g)


def GetDVehicle():
    if len(data) != 0:
        for d in data:
            i = d[0].startswith('D')
            if i:
                type_data.append(d)


def RemoveDVehicle():
    if len(data) != 0 and len(type_data) != 0:
        for d in data:
            i =d[0].startswith('D')
            if i:
                type_data.remove(d)

def GetZVehicle():
    if len(data) != 0:
        for z in data:
            i = z[0].startswith('Z')
            if i:
                type_data.append(z)


def RemoveZVehicle():
    if len(data) != 0 and len(type_data) != 0:
        for z in data:
            i = z[0].startswith('Z')
            if i:
                type_data.remove(z)

def GetTVehicle():
    if len(data) != 0:
        for t in data:
            i = t[0].startswith('T')
            if i:
                type_data.append(t)


def RemoveTVehicle():
    if len(data) != 0 and len(type_data) != 0:
        for t in data:
            i = t[0].startswith('T')
            if i:
                type_data.remove(t)

def GetKVehicle():
    if len(data) != 0:
        for k in data:
            i = k[0].startswith('K')
            if i:
                type_data.append(k)


def RemoveKVehicle():
    if len(data) != 0 and len(type_data) != 0:
        for k in data:
            i = k[0].startswith('K')
            if i:
                type_data.remove(k)